<?php
// +----------------------------------------------------------------------
// | HkCms 栏目管理模型
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2021 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\admin\model\cms;

use think\Model;
use think\model\concern\SoftDelete;

class Category extends Model
{
    use SoftDelete;

    /**
     * 追加属性
     * @param $value
     * @param $data
     * @return string
     */
    public function getTypeTextAttr($value,$data)
    {
        $option = ['category'=>lang('Category home'),'list'=>lang('Lists'),'link'=>lang('Other links')];
        return $option[$data['type']];
    }

    /**
     * 获取对应模型的ID，二维数组
     * @param $model_id
     * @return array
     */
    public function getModelCategory($model_id)
    {
        $category = $this->where(['status'=>'normal'])->select()->toArray();
        $category = \libs\Tree::instance()->init($category)->getTreeArray(0);
        $category = model_field_screen($model_id, $category);
        return \libs\Tree::instance()->getTreeList($category);
    }

    /**
     * 删除前的处理
     * @param Model $model
     * @return mixed|void
     * @throws \think\db\exception\DbException
     */
    public static function onBeforeDelete($model)
    {
        $data = $model->getData();

        // 文档数据删除
        if ($data['model_id'] && empty($data['delete_time'])) {
            $modelInfo = \app\admin\model\cms\Model::where(['id'=>$data['model_id']])->find();
            if (!empty($modelInfo)) {
                $c = '\app\admin\model\cms\\'.$modelInfo->controller;
                (new $c)->handleDel($modelInfo, $data['id']);
            }
        } else if ($data['model_id'] && !empty($data['delete_time'])) {
            $modelInfo = \app\admin\model\cms\Model::where(['id'=>$data['model_id']])->find();
            if (!empty($modelInfo)) {
                $c = '\app\admin\model\cms\\'.$modelInfo->controller;
                (new $c)->handleDel($modelInfo, $data['id'], 3);
            }
        }

        // 删除栏目权限
        \think\facade\Db::name('category_priv')->where(['category_id'=>$data['id']])->delete();
    }

    /**
     * 恢复后的处理
     * @param Model $model
     */
    public static function onAfterRestore($model)
    {
        $data = $model->getData();

        // 文档数据恢复
        if ($data['model_id']) {
            $tablename = \app\admin\model\cms\Model::where(['id'=>$data['model_id']])->value('tablename');
            $modelInfo = \app\admin\model\cms\Model::where(['id'=>$data['model_id']])->find();
            if (!empty($modelInfo)) {
                $c = '\app\admin\model\cms\\'.$modelInfo->controller;
                (new $c)->handleDel($modelInfo, $data['id'], 2);
            }
        }
    }
}
